﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Learn.Remoting.Server;
using System.Configuration;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Learn.Remoting.Client
{
    class Program
    {
        delegate int MyDelegate(int a, int b, int time);
        private static event MyDelegate a;
        private static MyDelegate md;
        static void Main(string[] args)
        {
            MyRemotingClient.ActivatedClient();
            //MyRemotingClient.WellKnownService();

            Console.ReadLine();
        }

        public static void FirstMethodInvokeRemoting()
        {
            #region 第一种方式掉用
            //MyRemoteObject app = (MyRemoteObject)Activator.GetObject(typeof(MyRemoteObject), ConfigurationSettings.AppSettings["RemoteServiceURL"]);
            //Console.WriteLine(app.Count());
            MyRemoteObject app = (MyRemoteObject)Activator.CreateInstance(typeof(MyRemoteObject), new object[] { 10 }, new object[] { new System.Runtime.Remoting.Activation.UrlAttribute(ConfigurationSettings.AppSettings["RemoteServiceURL"]) });
            MBV mbv = app.GetMBV();
            Console.WriteLine(mbv.Data);
            MBR mbr = app.GetMBR();
            Console.WriteLine(mbr.Data);
            mbv = new MBV(100);
            app.SetMBV(mbv);
            Console.WriteLine(app.UseMBV());
            mbr = new MBR(200);
            app.SetMBR(mbr);
            Console.WriteLine(app.UseMBR());


            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("test", typeof(System.String)));
            DataRow dr = dt.NewRow();
            dr["test"] = "data";
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
            ds = app.Method(ds);
            Console.WriteLine(ds.Tables[0].Rows[0]["test"].ToString());

            Console.ReadLine();
            #endregion
        }

        public static void SecondMethodInvokeRemoting()
        {  
           //RemotingConfiguration.RegisterActivatedClientType(
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemoteObject), "MyRemoteObject", WellKnownObjectMode.Singleton);
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 9999;
            TcpChannel channel = new TcpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel);
            Console.ReadLine(); 
        }

        public static void AsyncCallbackDemo()
        {
            DateTime dt = DateTime.Now;
            MyRemoteObject app = new MyRemoteObject();
            md = new MyDelegate(app.ALongTimeMethod);
            AsyncCallback ac = new AsyncCallback(Program.CallBack);

            IAsyncResult Iar = md.BeginInvoke(1, 2, 1000, ac, null);

            Method();

            //if (!Iar.IsCompleted)
            //{
            //    Iar.AsyncWaitHandle.WaitOne();
            //}
            //else
            //{
            //    Console.WriteLine("结果是" + md.EndInvoke(Iar));
            //}
            //Console.WriteLine("用了" + ((TimeSpan)(DateTime.Now - dt)).TotalSeconds + "秒");
        }

        public static void CallBack(IAsyncResult Iar)
        {
            if (Iar.IsCompleted)
            {
                Console.WriteLine("结果是" + md.EndInvoke(Iar));
            }
        }
        private static void Method()
        {
            Console.WriteLine("主线程方法开始");
            System.Threading.Thread.Sleep(3000);
            Console.WriteLine("主线程方法结束");
        }
    }
}
